package com.awesome.media;

import android.content.Context;

import java.util.HashMap;
import java.util.Map;

/**
 * Author: JFangZ
 * Email:janezhang1901@163.com
 * Date: 2020/4/29 14:20
 * Description:媒体播放器帮助类
 */
public enum MediaPlayerHelper {
    SINGLETON;

    private Map<Context, IMediaPlayer> mMediaPlayerMap = new HashMap<>();
    /**
     * Author: JFangZ
     * Email:qinhao@jeejio.com
     * Date:2020/9/1 15:23
     * Description:创建一个播放器实例
     */
    public IMediaPlayer createMediaPlayer(Context context, MediaPlayerType mediaPlayerType) {
        IMediaPlayer mediaPlayer = null;
        if (mediaPlayerType == MediaPlayerType.ANDROID) {
            mediaPlayer = new JeejioMediaPlayer(context);
        } else if (mediaPlayerType == MediaPlayerType.EXO) {
            mediaPlayer = new JeejioExoPlayer(context);
        } else if (mediaPlayerType == MediaPlayerType.IJK) {
            mediaPlayer = new JeejioIjkPlayer(context);
        } else {
            throw new IllegalArgumentException("mediaPlayerType " + mediaPlayerType + " is not support");
        }
        // 添加到管理集合中
        mMediaPlayerMap.put(context, mediaPlayer);
        return mediaPlayer;
    }

    public IMediaPlayer getMediaPlayer(Context context) {
        return mMediaPlayerMap.get(context);
    }

    public void removeMediaPlayer(Context context) {
        IMediaPlayer mediaPlayer = mMediaPlayerMap.remove(context);
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
        }
    }
}
